package com.oracle.controller;


import com.alibaba.fastjson.JSONArray;
import com.oracle.compents.ApiResponse;
import com.oracle.compents.CodeCompents;
import com.oracle.pojo.Command;
import com.oracle.pojo.Teacher;
import com.oracle.service.CommandService;
import com.oracle.service.LoginTeacherService;
import com.oracle.service.TeacherService;
import com.oracle.util.JJWTUtilsTeacher;
import com.oracle.vo.ImgVo;
import com.oracle.vo.TeacherVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/teacher")
public class TeacherController extends BaseTeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private LoginTeacherService loginTeacherService;

    @Autowired
    private CommandService commandService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("/login")
    public ApiResponse findOne(@RequestBody Teacher teacher){
        ApiResponse<String> response = loginTeacherService.login(teacher);
        if (!ObjectUtils.isEmpty(response)){
            response.setMsg(CodeCompents.ROLE_TEACHER+"");
            return response;
        }else {
            return null;
        }

    }

    @PostMapping("/register")
    public ApiResponse register(@RequestBody Teacher teacher){
        String teachCommand = teacher.getTeachCommand();
        Command oneCommand = commandService.findOneCommand(teachCommand);
        if (ObjectUtils.isEmpty(oneCommand)){
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("口令错误");
            return failed;
        }else {
            //对注册密码进行加密
            teacher.setTeachPwd(passwordEncoder.encode(teacher.getTeachPwd()));

            try {
                loginTeacherService.register(teacher);
            } catch (Exception e) {
                return ApiResponse.failed(null) ;
            }

            return ApiResponse.success(null);
        }

    }

    @PostMapping("/loginout1")
    public ApiResponse loginout1(@RequestBody Teacher teacher){
        Teacher user = getUser();
        loginTeacherService.loginOut(user);
        return ApiResponse.success(null);

    }

    @GetMapping("/findTeacher")
    public ApiResponse findTeacher(){
        Teacher user = getUser();
        System.out.println(user);
        return ApiResponse.success(user);
    }

    @PostMapping("/updata")
    public ApiResponse updata(@RequestBody TeacherVo teacherVo){
        System.out.println(teacherVo.getTeachImgList());
        List<String> teacherList = strToJson(teacherVo.getTeachImgList());

        //1.将视图对象的值 倒入到持久层对象中
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherVo,teacher);
        //2.Json格式转换
        String string = JSONArray.toJSONString(teacherList);
        //3.补充数据
        teacher.setTeachHeadList(string);
        int i = teacherService.updataTeacher(teacher);
        if (i>0){
            String teacherToken = JJWTUtilsTeacher.getJwtToken(teacher);
            ApiResponse<Object> success = ApiResponse.success(teacherToken);
            success.setMsg("教师修改信息成功");
            return success;
        }else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("修改失败");
            return failed;
        }
    }

    private List<String> strToJson(List<ImgVo> list){
        List<String> urlsList = new ArrayList<String>();
        //将goodsImgList数据进行json格式处理 => urljson数组
        list.forEach(x -> {
            urlsList.add(x.getUrl());
        });

        System.out.println("urls :" + urlsList);

        return urlsList;

    }

}
